﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System.Collections.Generic;
using Tesla.Math;

namespace Tesla.Content.Loaders.Model.Dxs {
    public class DxsPoly {
        private int _matID;
        private List<DxsVertex> _vertices;
        private Vector3 _faceNormal;

        public int MaterialID {
            get {
                return _matID;
            }
            set {
                _matID = value;
            }
        }

        public List<DxsVertex> Vertices {
            get {
                return _vertices;
            }
        }

        public Vector3 FaceNormal {
            get {
                return _faceNormal;
            }
        }

        public DxsPoly(int matID) {
            _matID = matID;
            _vertices = new List<DxsVertex>(3);
        }

        public int GetVertexID(int index) {
            if(index > _vertices.Count || index < 0) {
                return -1;
            }
            return _vertices[index].ID;
        }

        public void ComputeFaceNormal() {
            if(_vertices.Count != 3) {
                return;
            }
            Vector3 v1 = _vertices[1].Position - _vertices[0].Position;
            Vector3 v2 = _vertices[2].Position - _vertices[0].Position;

            _faceNormal = Vector3.Cross(v1, v2);
            _faceNormal.Normalize();
        }
    }
}
